package com.cashmama.market.api.application.controller.user.verfifycode;

import com.cashmama.market.api.application.annos.VisitorAccessible;
import com.cashmama.market.api.application.controller.user.verfifycode.request.VerifyCodeSendRequest;
import com.cashmama.market.api.domain.service.VerifyCodeService;
import com.cashmama.market.api.domain.vo.verifycode.VerifyCodeDTO;
import com.cashmama.market.api.domain.vo.RequestLocalInfo;
import com.cashmama.market.api.infras.handler.ApiResponse;
import com.fqgj.common.api.annotations.ParamsValidate;
import com.fqgj.common.utils.HttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @program: market-api
 * @description 发送短信
 * @author: frank
 * @create: 2019-12-20 10:42
 **/
@RestController
@RequestMapping("/verifycode")
public class VerifyCodeController {

    private static final Logger LOGGER = LoggerFactory.getLogger(VerifyCodeController.class);

    @Autowired
    private VerifyCodeService verifyCodeService;

    /**
     * 发送验证码
     *
     * @param verifyCodeSendRequest
     * @return
     */
    @ParamsValidate
    @VisitorAccessible
    @RequestMapping(value = "/send", method = RequestMethod.POST)
    public ApiResponse send(@RequestBody @Valid VerifyCodeSendRequest verifyCodeSendRequest) {

        VerifyCodeDTO verifyCodeDTO = new VerifyCodeDTO();
        BeanUtils.copyProperties(verifyCodeSendRequest, verifyCodeDTO);
        String userCode = RequestLocalInfo.getCurrentUser() == null ? "" : RequestLocalInfo.getCurrentUser().getUserCode();
        verifyCodeDTO.setUserCode(userCode);
        verifyCodeService.sendVerifyCode(verifyCodeDTO);

        return new ApiResponse<>("OTP send successfully");
    }
}
